package com.yzt.p_base.utils;

import android.app.Application;

import com.alibaba.fastjson.JSON;
import com.tencent.mmkv.MMKV;

import java.util.Map;

/**
 * author : bob
 * date   : 2022/11/29
 * desc   :
 */
public class SharedPreferenceUtil {

    /**
     * 保存在手机里面的文件名
     */
    private static final String SharedFILE_NAME = "zhuju";

    private static final String SharedFILE_SYSTEM_NAME = "zhuju_system";

    private static SharedPreferenceUtil preferenceUtils = null;

    protected SharedPreferenceUtil() {

    }

    public static void init(Application application) {
        try {
            if (application != null) {
                MMKV.initialize(application);
            }
        } catch (Exception e) {

        }
    }

    public static SharedPreferenceUtil getInstance() {
        if (preferenceUtils == null) {
            synchronized (SharedPreferenceUtil.class) {
                if (preferenceUtils == null) {
                    preferenceUtils = new SharedPreferenceUtil();
                }
            }
        }
        return preferenceUtils;
    }

    private void putRealize(String mapID, String key, Object value) {
        MMKV mmkv = MMKV.mmkvWithID(mapID);
        if (value instanceof String) {
            mmkv.encode(key, (String) value);
        } else if (value instanceof Integer) {
            mmkv.encode(key, (Integer) value);
        } else if (value instanceof Boolean) {
            mmkv.encode(key, (Boolean) value);
        } else if (value instanceof Float) {
            mmkv.encode(key, (Float) value);
        } else if (value instanceof Long) {
            mmkv.encode(key, (Long) value);
        }
    }

    /**
     * 存入某个key对应的value值
     */
    public void putValue(String key, Object value) {
        putRealize(SharedFILE_NAME, key, value);
    }

    public void putValue(String key, Object value, String sharedFileName) {
        putRealize(sharedFileName, key, value);
    }

    /**
     * 存储系统默认值
     */
    public void putSystemValue(String key, Object value) {
        putRealize(SharedFILE_SYSTEM_NAME, key, value);
    }


    /**
     * 得到某个key对应的值
     */
    public <T> T getValue(String key, T defValue) {
        MMKV mmkv = MMKV.mmkvWithID(SharedFILE_NAME);

        T res;
        try {
            if (defValue instanceof Integer) {
                res = (T) Integer.valueOf(mmkv.decodeInt(key, (Integer) defValue));
            } else if (defValue instanceof Boolean) {
                res = (T) Boolean.valueOf(mmkv.decodeBool(key, (Boolean) defValue));
            } else if (defValue instanceof Float) {
                res = (T) Float.valueOf(mmkv.decodeFloat(key, (Float) defValue));
            } else if (defValue instanceof Long) {
                res = (T) Long.valueOf(mmkv.decodeLong(key, (Long) defValue));
            } else {
                if (defValue instanceof String) {
                    res = (T) mmkv.decodeString(key, (String) defValue);
                } else {
                    String sss = mmkv.decodeString(key, "");
                    res = (T) JSON.parseObject(sss, defValue.getClass());
                }
            }
        } catch (Exception e) {
            res = defValue;
        }
        return res;
    }

    /**
     * 得到某个key对应的值
     */
    public Object getValue(String key, Object defValue, String sharedFileName) {
        MMKV mmkv = MMKV.mmkvWithID(sharedFileName);

        if (defValue instanceof String) {
            return mmkv.decodeString(key, (String) defValue);
        } else if (defValue instanceof Integer) {
            return mmkv.decodeInt(key, (Integer) defValue);
        } else if (defValue instanceof Boolean) {
            return mmkv.decodeBool(key, (Boolean) defValue);
        } else if (defValue instanceof Float) {
            return mmkv.decodeFloat(key, (Float) defValue);
        } else if (defValue instanceof Long) {
            return mmkv.decodeLong(key, (Long) defValue);
        }
        return null;
    }

    /**
     * 得到某个key对应的值
     */
    public Object getSystemValue(String key, Object defValue) {
        return getValue(key, defValue, SharedFILE_SYSTEM_NAME);
    }

    /**
     * 移除某个key值已经对应的值
     */
    public void remove(String key) {
        MMKV.mmkvWithID(SharedFILE_NAME).removeValueForKey(key);
    }

    /**
     * 清除实例数据
     */
    public void clearAllData(String mmapID) {
        MMKV.mmkvWithID(mmapID).clearAll();
    }

    /**
     * 查询某个key是否已经存在
     */
    public boolean contains(String key) {
        return MMKV.mmkvWithID(SharedFILE_NAME).contains(key);
    }

    /**
     * 返回所有的键值对
     */
    public Map<String, ?> getAll() {
        return MMKV.mmkvWithID(SharedFILE_NAME).getAll();
    }
}

